import {Component, OnInit} from '@angular/core';
import {NavController, NavParams, ViewController} from 'ionic-angular';
import {AppPopup} from "../../../util/popup";
import {StorageService} from "../../../providers/storage-service";


@Component({
    selector: 'kcmodel', templateUrl: 'kcmodel.html'
})
export class KcmodelPage implements OnInit {

    btnAct = true;
    thisProductInfo;
    productId;

    jxs_issuelist;
    jxs_receivelist;
    jxs;
    keyword = '';
    receivedCompanyId;

    constructor(public navCtrl: NavController,
                public params: NavParams,
                public viewCtrl: ViewController,
                private popup: AppPopup,
                private storageServ: StorageService,) {
        console.log('thisProductInfo', params.get('thisProductInfo'));
        this.thisProductInfo = params.get('thisProductInfo');
        this.productId = this.thisProductInfo.productId;
    }

    cancelModel() {

    }

    dismiss() {
        this.viewCtrl.dismiss();
    }

    //发货渠道按钮事件
    btn1Click() {
        this.btnAct = true;
        this.issuelist();
    }

    //收货渠道按钮事件
    btn2Click() {
        this.btnAct = false;
        this.receivelist();
    }

    //搜索
    search() {
        if (this.btnAct) {
            this.issuelist();
        } else {
            this.receivelist();
        }
    }

    //清空搜索
    clearInput() {
        this.keyword = "";
        this.search();
    }

    ngOnInit() {
        this.issuelist();
    }

    //发货渠道
    issuelist() {
        let param = {
            "keyword": this.keyword, "productId": this.productId
        }
        this.storageServ.issuelist(param)
            .then(res => {
                console.info("发货渠道查询成功：", res);
                this.jxs_issuelist = res.infoData.items;
            })
            .catch(err => {
                console.error("发货渠道查询失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    //收货渠道
    receivelist() {
        let param = {
            "isout": "1", "keyword": this.keyword, "productId": this.productId
        }
        this.storageServ.receivelist(param)
            .then(res => {
                console.info("收货渠道查询成功：", res);
                this.jxs_receivelist = res.infoData.items;
            })
            .catch(err => {
                console.error("收货渠道查询失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    //点击选择
    toManagement(index) {
        if (this.btnAct) {
            this.jxs = this.jxs_issuelist[index];
            this.receivedCompanyId = this.jxs.receivedCompanyId;
        } else {
            this.jxs = this.jxs_receivelist[index];
            this.receivedCompanyId = this.jxs.sendCompanyId;
        }
        this.viewCtrl.dismiss(this.jxs, this.receivedCompanyId);
    }

}
